<template>
  <el-container>
    <el-main>
      <el-form :model="form.model" :rules="form.rules" ref="formRef" label-width="100px">
          <el-form-item label="分类名称">
            <el-input v-model="form.model.name" placeholder="请输入"></el-input>
          </el-form-item>
      </el-form>
    </el-main>
    <el-footer class="sa-footer--submit">
      <el-button v-if="props.modal.params.type=='add'" type="primary" @click="confirm"
        >保存</el-button>
      <el-button v-if="props.modal.params.type=='again'" type="primary" @click="confirm"
      >重新提交</el-button>
    </el-footer>
  </el-container>
</template>
<script setup>
  import { onMounted, reactive, ref, unref } from 'vue';
  import { api } from '../task.service';
  import { cloneDeep } from 'lodash';



  const props = defineProps({
    modal: {
      type: Object,
    },
  });

  const emit = defineEmits(['modalCallBack']);
 
  // 添加 编辑 form
  let formRef = ref(null);

  const form = reactive({
    model: {
     name:'',
    },
    rules:{},
  });
  const loading = ref(false);

  // 获取详情
  async function getDetail(id) {
    loading.value = true;
    const { error, data } = await api.real.detail(id);
    if(error === 0){
      form.model = data
      loading.value = false;
    
    }
  }
  // 表单关闭时提交
  async function confirm() {
    // 表单验证
    unref(formRef).validate(async (valid) => {
      if (!valid) return;
      let submitForm = cloneDeep(form.model);
      submitForm={
        ...submitForm,
      }
      return false;
      const { error } =
        props.modal.params.type == 'add'||props.modal.params.type == 'again'
          ? await api.real.add(submitForm)
          : ''
      if (error == 0) {
        emit('modalCallBack', { event: 'confirm' });
      }
    });
  }


  async function init() {
    if (props.modal.params.id) {
      await getDetail(props.modal.params.id);
    }
  }
  onMounted(() => {
    init();
  });
</script>
<style lang="scss" scoped>
div{
  font-size:13px;
}
.left-t{
  color: #777777;
  text-align: right;
  flex-shrink: 0;
  word-break:keep-all;
}
.title{
  font-size:18px;
  margin:15px 0 15px 20px;
}
.line{
  line-height: 2em;
  align-items: start;
}

</style>